package com.oa.pm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.joneying.common.annotation.CommonService;
import com.oa.core.PageResult;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import com.oa.core.utils.request.RFormParam;
import com.oa.pm.dto.InductionTrainingDTO;
import com.oa.pm.entity.InductionTraining;
import com.oa.pm.entity.InductionTrainingPersonnel;
import com.oa.pm.mapper.InductionTrainingMapper;
import com.oa.pm.mapper.InductionTrainingPersonnelMapper;
import com.oa.pm.service.IInductionTrainingPersonnelService;
import com.oa.pm.service.IInductionTrainingService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.pm.vo.DimissionManagementDetailsVO;
import com.oa.pm.vo.InductionTrainingDetailsVO;
import com.oa.pm.vo.InductionTrainingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 入职培训表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class InductionTrainingServiceImpl extends ServiceImpl<InductionTrainingMapper, InductionTraining> implements IInductionTrainingService {

    private final InductionTrainingMapper inductionTrainingMapper;

    @Autowired
    private IInductionTrainingPersonnelService inductionTrainingPersonnelService;

    @Autowired
    public InductionTrainingServiceImpl(InductionTrainingMapper inductionTrainingMapper) {
        this.inductionTrainingMapper = inductionTrainingMapper;
    }

    @Override
    @CommonService
    public PageResult<InductionTraining> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<InductionTraining> list = inductionTrainingMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<InductionTraining> page = new Page<>(pageNum, pageSize);
            //查询
            List<InductionTraining> list = inductionTrainingMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public InductionTrainingDetailsVO findInductionTrainingById(String id){
        return inductionTrainingMapper.findInductionTrainingById(id);
    }

    @Override
    public PageResult<InductionTrainingVO> findInductionTrainingList(RFormParam param) {
        return new PageResult<>(inductionTrainingMapper.findInductionTrainingList(param.getPage(), param.getParamList()));
    }

    @Transactional
    @Override
    public boolean addInductionTraining(InductionTrainingDTO inductionTrainingDTO) {
        if(insert(inductionTrainingDTO.getInductionTraining())){
            List<InductionTrainingPersonnel> inductionTrainingPersonnels = inductionTrainingDTO.getInductionTrainingPersonnels();
            inductionTrainingPersonnels.forEach(d -> {
                d.setTrainingId(inductionTrainingDTO.getInductionTraining().getId());
                d.setType(1);
            });
            if(inductionTrainingPersonnelService.insertBatch(inductionTrainingPersonnels)){
                return true;
            }
        }
        return false;
    }

    @Transactional
    @Override
    public boolean editInductionTraining(InductionTrainingDTO inductionTrainingDTO) {
        if(updateById(inductionTrainingDTO.getInductionTraining())){
            Wrapper wrapper = new EntityWrapper();
            wrapper.where("training_id = {0} and type = 1", inductionTrainingDTO.getInductionTraining().getId());
            if(inductionTrainingPersonnelService.delete(wrapper)){
                List<InductionTrainingPersonnel> inductionTrainingPersonnels = inductionTrainingDTO.getInductionTrainingPersonnels();
                inductionTrainingPersonnels.forEach(d -> {
                    d.setTrainingId(inductionTrainingDTO.getInductionTraining().getId());
                    d.setType(1);
                });
                if(inductionTrainingPersonnelService.insertBatch(inductionTrainingPersonnels)){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void export(RFormParam formParam, HttpServletResponse response) {
        List<InductionTrainingVO> inductionTrainingVOS;
        if(formParam.getPage() != null){
            inductionTrainingVOS = inductionTrainingMapper.findInductionTrainingList(formParam.getPage(), formParam.getParamList());
        }else{
            inductionTrainingVOS = inductionTrainingMapper.findInductionTrainingList(new Page(), formParam.getParamList());
        }
        inductionTrainingVOS.forEach(d -> d.setUserList(null));
        String[] rows = {"id", "培训时长", "培训地点", "培训人员", "培训方式"};
        try {
            ExcelUtil.downloadExcel("入职培训记录" + DateUtil.getDayMonth(), rows, inductionTrainingVOS, "入职培训记录" + DateUtil.getDayMonth()+".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
